<template>
  <section class="war-list-main">
    <el-collapse-transition>
      <div class="course-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.pkName"
          placeholder="对战名称"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <div class="course-search-btn">
          <el-button
            class="filter-item"
            type="primary"
            @click="handleFilter"
          >搜索</el-button>
          <el-button
            class="filter-item"
            type="primary"
            @click="handleReset"
          >重置</el-button>
        </div>
      </div>
    </el-collapse-transition>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>对战管理</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="handleAdd">新增</el-button>
      </section>
    </section>

    <section class="main-table">
      <v-table
        :list="list"
        tag="warList"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
      >
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          min-width="100"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleUpdate(scope.row)"
            >{{ filterStatus(scope.row.status) }}</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleToRobot(scope.row)"
            >机器人</el-button>
            <el-button
              v-if="scope.row.isApply ===1 "
              type="text"
              size="mini"
              @click="goApplication(scope.row)"
            >报名信息</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleView(scope.row)"
            >查看地址</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
  </section>
</template>
<script>
import { getDictByCode, queryPage, updateStatus, deleteById } from '../api'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'WarList',
  components: {
    VTable,
    Pagination
  },
  directives: {
    elDragDialog
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        pkName: ''
      },
      list: [{ status: 'ZT01' }],
      total: 0,
      listLoading: false,
      dict: {
        ZT: [] // 状态
      },
      link: ''
    }
  },
  watch: {},
  created() {
    this.getList()
    for (const key in this.dict) {
      this.getDict(key)
    }
  },
  methods: {

    // 前往报名信息
    goApplication(row) {
      this.$router.push({
        path: this.$filterPath('application'),
        query: {
          id: row.id
        }
      })
    },
    handleView(row) {
      if (row.pkType === 'PKLX_SRS') {
        this.link = '/answerContest/SRS'
      } else if (row.pkType === 'PKLX_SRDZ') {
        this.link = '/answerContest/SRDZ'
      }
      this.$confirm(`链接地址为：${this.link}<span style="color:red">注意：该链接只对已发布数据有效</span>`, '提示', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '复制链接',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const input = document.createElement('input')
        input.setAttribute('readonly', 'readonly')
        input.setAttribute('value', this.link)
        document.body.appendChild(input)
        input.select()
        if (document.execCommand('copy')) {
          document.execCommand('copy')
          this.$message({
            title: '成功',
            message: '复制成功！',
            type: 'success',
            duration: 1500
          })
        }
        document.body.removeChild(input)
      }).catch(() => {

      })
    },
    /**
     * @name: 获取字典
     * @test: test font
     * @msg:
     * @return {*}
     */
    /**
     *
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code)
          .then((res) => {
            this.dict[code] =
              res.data.data && res.data.data.length > 0
                ? res.data.data[0].list
                : []
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    /**
     * @name: 状态格式化
     * @test: test font
     * @msg:
     * @param {*} s
     * @return {*}
     */
    filterStatus(s) {
      return this.dict.ZT && this.dict.ZT[
        this.dict.ZT.map((item) => item.dictValue).indexOf(s) ^ 1
      ]?.dictName || ''
    },
    /**
     * @name: 新增
     * @test: test font
     * @msg:
     * @return {*}
     */
    handleAdd() {
      this.$router.push({
        path: this.$filterPath('warManage'),
        query: {
          type: 'c'
        }
      })
    },
    /**
     * @name: 启用/禁用
     * @test: test font
     * @msg:
     * @param {*} row
     * @return {*}
     */
    handleUpdate(row) {
      const key = this.dict.ZT.map((item) => item.dictValue).indexOf(row.status) ^ 1
      const msg = this.dict.ZT[key]?.dictName
      const newStatus = this.dict.ZT[key].dictValue
      this.$confirm('确定' + msg + '此对战吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            updateStatus({ id: row.id, status: newStatus })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: msg + '成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch()
    },
    /**
     * @name: 编辑
     * @test: test font
     * @msg:
     * @return {*}
     */
    handleEdit(row) {
      this.$router.push({
        path: this.$filterPath('warManage'),
        query: {
          i: row.id,
          type: 'u'
        }
      })
    },
    /**
     * @name: 前往机器人
     * @test: test font
     * @msg:
     * @return {*}
     */
    handleToRobot(row) {
      this.$router.push({
        path: this.$filterPath('robotList'),
        query: {
          fkInfoId: row.id
        }
      })
    },
    /**
     * @name: 删除
     * @test: test font
     * @msg:
     * @return {*}
     */
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            deleteById({ id: row.id })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    filtercolumn(scope, item) {
      if (item === 'limitDayTimes' && scope.row[item] == -1) {
        return '无限制'
      }
      return scope.row[item]
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.pkName = ''
      for (const k in this.search) {
        this.search[k] = ''
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: this.listQuery.page,
        pageSize: this.listQuery.limit,
        pkName: this.listQuery.pkName
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.list = response.data.data.rows || []
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    }
  }
}
</script>
<style lang="scss">
$dark_gray: #bfced6;

.war-list-main {
  padding: 10px;

  .el-dialog .el-dialog__body {
    max-height: 73vh !important;
  }

  .line {
    text-align: center;
  }

  .course-search {
    background-color: #fff;
    padding: 0 10px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .course-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .course-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .course-search-inner {
        padding-top: 10px;
        // .el-input--medium .el-input__inner {
        // 	padding-left: 30px !important;
        // }
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .course-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin: 10px 0;

    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);

      .num {
        @include font-color($mainColor);
      }
    }
  }
}
</style>
